# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        res = 0
        l3 = ListNode()
        cur = l3
        while (l1 or l2):
            if (l1):
                res += l1.val  # res=2
                l1 = l1.next
            if (l2):
                res += l2.val  # res=2+5=7
                l2 = l2.next
            # res=10, val=0, res=>val val=res%10
            # res=14, val=4, 14%10=4
            l3.next = ListNode(res % 10)
            l3 = l3.next
            # res=10, 进位为1， 10//10=1
            # res=14, 进位为1， 14//10=1
            res //= 10
        if res == 1:
            l3.next = ListNode(1)
        return cur.next